package com.thirteenClassicsServer.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.thirteenClassics.dto.ImportCatalogDTO;
import com.thirteenClassics.dto.PictureCatalogQueryDTO;
import com.thirteenClassics.entity.Catalog;
import com.thirteenClassics.exception.BaseException;
import com.thirteenClassicsServer.mapper.CatalogBookMapper;
import com.thirteenClassicsServer.mapper.PictureMapper;
import com.thirteenClassicsServer.service.IBookService;
import com.thirteenClassicsServer.service.ICatalogBookService;

@Service
public class CatalogBookServiceImpl implements ICatalogBookService {

    @Resource
    private PictureMapper pictureMapper;

    @Resource
    private CatalogBookMapper catalogBookMapper;

    @Resource
    private IBookService bookService;


    @Override
    public void insertcatalog(PictureCatalogQueryDTO pictureCatalogQueryDTO) {
        List<Catalog> lists = pictureMapper.selectMinorityCatalogOrdersByName(pictureCatalogQueryDTO);
        boolean flag = false;
        for (Catalog catalog : lists) {
            ImportCatalogDTO importCatalogDTO = new ImportCatalogDTO();
            //importCatalogDTO.setCatalogId(catalog.getId());
            //importCatalogDTO.setBook(pictureCatalogQueryDTO.getBookId());
            String catalogProcess = catalog.getAncestors();
            importCatalogDTO.setCatalog(catalogProcess);
            //importCatalogDTO.setBookLang(pictureCatalogQueryDTO.getBookLang());
            importCatalogDTO.setBookName(pictureCatalogQueryDTO.getBookName());
            //catalogBookMapper.insertCatalogBook(importCatalogDTO);
            importCatalogDTO.setEntryId(catalog.getId());
            //todo
            importCatalogDTO.setBookLang2(pictureCatalogQueryDTO.getBookLang());
            importCatalogDTO.setBookLang(pictureCatalogQueryDTO.getBookLang() + ' ');
            int inf = pictureMapper.updataEntryIdByBookNameAndBookLang(importCatalogDTO);
            if (inf == 0) {
                flag = true;
            }

            System.out.print("链接图片：" + importCatalogDTO);
        }
        if (flag) {
            throw new BaseException("该书目没有对应图片");
        }

    }

    @Override
    public void insertcatalogJdb(PictureCatalogQueryDTO pictureCatalogQueryDTO) {
        List<Catalog> lists = pictureMapper.selectCatalogOrdersByName(pictureCatalogQueryDTO);
        for (Catalog catalog : lists) {
            ImportCatalogDTO importCatalogDTO = new ImportCatalogDTO();
            importCatalogDTO.setCatalogId(catalog.getId());
            importCatalogDTO.setBook(pictureCatalogQueryDTO.getBookId());
            importCatalogDTO.setCatalog(catalog.getAncestors());
            catalogBookMapper.insertCatalogBook(importCatalogDTO);
        }
    }

    @Override
    public int updateMinorityBkId(String bookName, String bookLang, Long id) {
        return catalogBookMapper.updateMinorityBkId(bookName, bookLang, id);
    }
}
